//
//  PersonInCchargeShowView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/5.
//

#import "PersonInCchargeShowView.h"
#import "getPondTissueListModel.h"
#import "UIScrollView+EmptyDataSet.h"

@interface PersonInCchargeShowView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UIView * makeView;
@property (nonatomic ,assign) CGFloat viewH;
@property (nonatomic ,strong) UITableView * lefttableview;
@property (nonatomic ,strong) UITableView * righttableview;
@property (nonatomic ,strong) UILabel * seledCount;
@property (nonatomic ,strong) NSArray <PositionUserModel *> * array;
@property (nonatomic ,strong) NSMutableArray <PondUserModel *> * Seledarray;
@property (nonatomic ,strong) void(^block)(NSArray <PondUserModel *>* array);
@end

@implementation PersonInCchargeShowView
- (NSMutableArray<PondUserModel *> *)Seledarray{
    if(!_Seledarray){
        _Seledarray = [NSMutableArray array];
    }
    return _Seledarray;
}
+ (instancetype)showviewWithArray:(NSArray <PositionUserModel *>*)array andBlock:(void (^)(NSArray<PondUserModel *> * _Nonnull))block{
    PersonInCchargeShowView * view = [PersonInCchargeShowView new];
    array.firstObject.isSeled = YES;
    view.array = array;
    for (PositionUserModel * model in array) {
        for (PondUserModel * usermodel in model.userModelList) {
            if(usermodel.isSeled){
                BOOL ishave = NO;
                for (PondUserModel * oldModel in view.Seledarray) {
                    if([oldModel.userId isEqualToString:usermodel.userId]){
                        ishave = YES;
                    }
                }
                if(ishave == NO){
                    [view.Seledarray addObject:usermodel];
                }
            }
        }
    }
    view.block = block;
    [view setupview];
    return view;
}
- (void)setupview{
    [[DTKTool getCurrentVC].view addSubview:self];
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.viewH = kwt(500) + KSafeAreaBottomHeight;
    UIView * makeView = [UIView new];
    self.makeView = makeView;
    makeView.backgroundColor = KcolorHex(@"#EDF2F8");
    makeView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, kwt(170) + KSafeAreaBottomHeight);
    [self addSubview:makeView];
    [DTKTool BFYJ:kwt(15) andView:makeView andCorner:UIRectCornerTopLeft|UIRectCornerTopRight andSize: CGRectMake(0, 0 ,SCREEN_WIDTH, self.viewH + KSafeAreaBottomHeight)];
    
//    view.userInteractionEnabled = YES;
//    [view addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:view action:@selector(dismiss)]];
//    makeView.userInteractionEnabled = YES;
//    [makeView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(noclick)]];
//    self.userInteractionEnabled = YES;
//    [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)]];
//    makeView.userInteractionEnabled = YES;
//    [makeView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(noclick)]];
    
    UILabel * titlab = [DTKTool labelWithText:@"任务指派人" andFont:kCFont(18) andTextColor:KcolorHex(@"#242729")];
    [makeView addSubview:titlab];
    [titlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.top.offset(kwt(15));
    }];
    UILabel * titlab1 = [DTKTool labelWithText:@"（最多8个）" andFont:kFont(18) andTextColor:KcolorHex(@"#242729")];
    [makeView addSubview:titlab1];
    [titlab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titlab.mas_right);
        make.centerY.equalTo(titlab);
    }];
    UIButton * btn = [UIButton buttonWithType:0];
    [btn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    btn.addTarget(self,@selector(dismiss));
    [makeView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-15));
        make.width.height.offset(kwt(20));
        make.centerY.equalTo(titlab);
    }];
  
    UIView * buttonview = [UIView new];
    [makeView addSubview:buttonview];
    buttonview.backgroundColor = UIColor.whiteColor;
    [buttonview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(68) + KSafeAreaBottomHeight);
    }];
    UILabel * temp1 = [DTKTool labelWithText:@"已选" andFont:kFont(15) andTextColor:KcolorHex(@"#5A6066")];
    [buttonview addSubview:temp1];
    [temp1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.top.offset(kwt(26));
    }];
    UILabel * seledCount = [DTKTool labelWithText:@"0" andFont:kFont(15) andTextColor:KcolorHex(@"#0967FF")];
    self.seledCount = seledCount;
    [buttonview addSubview:seledCount];
    [seledCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(temp1.mas_right).offset(kwt(2));
        make.centerY.equalTo(temp1);
    }];
    
    UILabel * temp3 = [DTKTool labelWithText:@"个" andFont:kFont(15) andTextColor:KcolorHex(@"#5A6066")];
    [buttonview addSubview:temp3];
    [temp3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(seledCount.mas_right).offset(kwt(2));
        make.centerY.equalTo(temp1);
    }];
    UIButton * qxbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qxbtn.backgroundColor = KcolorHex(@"#F0F2F4");
    [qxbtn setTitle:@"清空" forState:UIControlStateNormal];
    [qxbtn setTitleColor:KcolorHex(@"#5A6066") forState:UIControlStateNormal];
    qxbtn.titleLabel.font = kFont(17);
    [DTKTool YJ:kwt(5) andView:qxbtn];
    [qxbtn addTarget:self action:@selector(leftBtnclick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:qxbtn];
    [qxbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(44));
        make.width.offset(kwt(116));
        make.left.offset(kwt(118));
        make.top.offset(kwt(12));
    }];
    
    UIButton * qrbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qrbtn.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(160), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#2892FE") endColor:KcolorHex(@"#0366FE")];
    [qrbtn setTitle:@"确定" forState:UIControlStateNormal];
    [qrbtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    qrbtn.titleLabel.font = kCFont(17);
    [DTKTool YJ:kwt(5) andView:qrbtn];
    [qrbtn addTarget:self action:@selector(rightbtnclick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:qrbtn];
    [qrbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(44));
        make.width.offset(kwt(116));
        make.right.offset(kwt(-15));
        make.top.offset(kwt(12));
    }];
    
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#D8D8D8");
    [makeView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(53));
        make.left.right.offset(0);
        make.height.offset(0.5);
    }];
    
  
    
    UITableView * lefttableview = [UITableView new];
    lefttableview.delegate = self;
    lefttableview.dataSource = self;
    lefttableview.backgroundColor = KcolorHex(@"#F0F3F9");
    [makeView addSubview:lefttableview];
    self.lefttableview = lefttableview;
    [lefttableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.width.offset(kwt(115));
        make.top.equalTo(line.mas_bottom);
        make.bottom.equalTo(buttonview.mas_top);
    }];
    
    
    UITableView * righttableview = [UITableView new];
    righttableview.delegate = self;
    righttableview.dataSource = self;
    [makeView addSubview:righttableview];
    self.righttableview = righttableview;
    [righttableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(0);
        make.left.equalTo(lefttableview.mas_right);
        make.top.equalTo(line.mas_bottom);
        make.bottom.equalTo(buttonview.mas_top);
    }];
    
    [self ReloadUI];
    
    [self show];
    
}


- (PositionUserModel *)seledModel{
    for (PositionUserModel * model in self.array) {
        if(model.isSeled){
            return model;
        }
    }
    return self.array.firstObject;
}
- (void)ReloadUI{
    self.seledCount.text = [NSString stringWithFormat:@"%lu",(unsigned long)self.Seledarray.count];
    [self.lefttableview reloadData];
    [self.righttableview reloadData];
}

- (void)leftBtnclick{
    for (PositionUserModel * model in self.array) {
        for (PondUserModel * usermodel in model.userModelList) {
            usermodel.isSeled = NO;
        }
    }
    [self.Seledarray removeAllObjects];
    [self ReloadUI];
    
}

- (void)rightbtnclick{
    if(self.Seledarray.count < 1){
        [self showWarning:@"至少选择一人"];
        return;
    }
    if(self.block){
        self.block(self.Seledarray);
    }
    [self dismiss];
}


- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableView == self.lefttableview){
        return self.array.count;
    } else {
        return [self seledModel].userModelList.count;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kwt(53);
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MJWeakSelf
    if(tableView == self.lefttableview){
        
        PersonInCchargeLeftTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PersonInCchargeLeftTableViewCell"];
        if (cell == nil) {
            cell = [[PersonInCchargeLeftTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PersonInCchargeLeftTableViewCell"];
        }
        cell.model = self.array[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell ;
    } else {
        PersonInCchargeRightTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PersonInCchargeRightTableViewCell"];
        if (cell == nil) {
            cell = [[PersonInCchargeRightTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PersonInCchargeRightTableViewCell"];
        }
        cell.model = [self seledModel].userModelList[indexPath.row];
        BOOL isSeled = NO;
        for (PondUserModel * usermodel in self.Seledarray) {
            if([[self seledModel].userModelList[indexPath.row].userId isEqualToString:usermodel.userId]){
                isSeled = YES;
            }
        }
        cell.isSeled = isSeled;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell ;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView == self.lefttableview){
        for (PositionUserModel * model in self.array) {
            model.isSeled = NO;
        }
        self.array[indexPath.row].isSeled = YES;
    } else {
        PondUserModel * model = [self seledModel].userModelList[indexPath.row];
        BOOL isSeled = NO;
        for (PondUserModel * usermodel in self.Seledarray) {
            if([[self seledModel].userModelList[indexPath.row].userId isEqualToString:usermodel.userId]){
                isSeled = YES;
            }
        }
        if(self.Seledarray.count > 7 && isSeled == NO){
            [self showWarning:@"最多8人"];
            return;
        }
        if(isSeled){
            PondUserModel * tempModel;
            for (PondUserModel * seledmodel in self.Seledarray) {
                if([model.userId isEqualToString:seledmodel.userId]){
                    tempModel = seledmodel;
                }
            }
            [self.Seledarray removeObject:tempModel];
        } else {
            [self.Seledarray addObject:model];
        }
        
    }
    [self ReloadUI];
    
}
- (void)show{
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT - self.viewH , SCREEN_WIDTH, self.viewH);
    }];
}
- (void)noclick{}
- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT , SCREEN_WIDTH, self.viewH);
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}
@end


@interface PersonInCchargeLeftTableViewCell ()
@property (nonatomic ,strong) UILabel * name;
@property (nonatomic ,strong) UIView * line;
@property (nonatomic ,strong) UIView * backview;
@end

@implementation PersonInCchargeLeftTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    UIView * backview = [UIView new];
    backview.backgroundColor = KcolorHex(@"#F0F3F9");
    self.backview = backview;
    [self.contentView addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#F0F3F9");
    self.line = line;
    [backview addSubview:line];
    [DTKTool YJ:kwt(2) andView:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.centerY.offset(0);
        make.width.offset(kwt(4));
        make.height.offset(kwt(35));
    }];
    
    UILabel * name = [DTKTool labelWithText:@"-" andFont:kFont(17) andTextColor:KcolorHex(@"#222222")];
    self.name = name;
    name.textAlignment = NSTextAlignmentCenter;
    [backview addSubview:name];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
        make.width.offset(kwt(100));
    }];
    
}
- (void)setModel:(PositionUserModel *)model{
    self.name.text = model.name;
    if(model.isSeled){
        self.backview.backgroundColor = UIColor.whiteColor;
        self.line.backgroundColor = KcolorHex(@"#0967FF");
        self.name.textColor = KcolorHex(@"#0967FF");
        self.name.font = kCFont(17);
    } else {
        self.backview.backgroundColor = UIColor.clearColor;
        self.line.backgroundColor = KcolorHex(@"#FFFFFF");
        self.name.textColor = KcolorHex(@"#222222");
        self.name.font = kFont(17);
    }
}

@end


@interface PersonInCchargeRightTableViewCell ()
@property (nonatomic ,strong) UIImageView * userimg;
@property (nonatomic ,strong) UIImageView * gimg;
@property (nonatomic ,strong) UILabel * namelab;

@end

@implementation PersonInCchargeRightTableViewCell



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.whiteColor;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    UIImageView * userimg = kimageView(@"unslete_people");
    self.userimg= userimg;
    [self.contentView addSubview:userimg];
    [userimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(22));
        make.centerY.offset(0);
        make.left.offset(kwt(12));
    }];
    
    UIImageView * gimg = kimageView(@"unslete_people");
    self.gimg= gimg;
    [self.contentView addSubview:gimg];
    [gimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(22));
        make.centerY.offset(0);
        make.right.offset(kwt(-15));
    }];
    
    UILabel * namelab = [DTKTool labelWithText:@"-" andFont:kFont(17) andTextColor:KcolorHex(@"#222222")];
    self.namelab = namelab;
    [self.contentView addSubview:namelab];
    [namelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(kwt(42));
        make.right.offset(kwt(-42));
    }];
    
}

- (void)setModel:(PondUserModel *)model{
    self.namelab.text = [NSString stringWithFormat:@"%@ %@",model.realName,model.phone];
}
- (void)setIsSeled:(BOOL)isSeled{
    _isSeled = isSeled;
    if(isSeled){
        self.userimg.image = [UIImage imageNamed:@"slete_people"];
        self.gimg.image = [UIImage imageNamed:@"departmentSeled"];
        self.namelab.textColor = KcolorHex(@"#0967FF");
        [DTKTool YJBK:kwt(11) andView:self.gimg andWidth:0 andColor:KcolorHex(@"#CECECE")];

    } else {
        self.namelab.textColor = KcolorHex(@"#222222");
        self.userimg.image = [UIImage imageNamed:@"unslete_people"];
        if(self.isDX){
            self.gimg.image = [UIImage imageNamed:@""];
            [DTKTool YJBK:kwt(11) andView:self.gimg andWidth:1 andColor:KcolorHex(@"#CECECE")];
        } else {
            self.gimg.image = [UIImage imageNamed:@"departmentNoSeled"];

        }
    
    }
}
@end

@interface SearchPersonInCchargeShowView ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (nonatomic ,strong) UIView * makeView;
@property (nonatomic ,assign) CGFloat viewH;
@property (nonatomic ,strong) UITableView * lefttableview;
@property (nonatomic ,strong) UITableView * righttableview;
@property (nonatomic ,strong) UITableView * sstableview;
@property (nonatomic ,strong) NSString * title;
@property (nonatomic ,strong) NSString * ssStr;
@property (nonatomic ,strong) NSArray <PositionUserModel *> * array;
///选中的ID
@property (nonatomic ,strong) NSString * seledID;
@property (nonatomic ,strong) NSMutableArray <PondUserModel *> * Seledarray;
@property (nonatomic ,strong) NSMutableArray <PondUserModel *> * ssArray;
@property (nonatomic ,strong) UITextField * sstextfield;
@property (nonatomic ,strong) void(^block)(NSString * ID);
@property (nonatomic ,strong) UIView * ssview;
@property (nonatomic ,strong) UIButton * textRemovebtn;
@property (nonatomic ,strong) UIButton * qxBtn;
@property (nonatomic ,strong) UIButton * qdBtn;
@end

@implementation SearchPersonInCchargeShowView
- (NSMutableArray<PondUserModel *> *)Seledarray{
    if(!_Seledarray){
        _Seledarray = [NSMutableArray array];
    }
    return _Seledarray;
}
- (NSMutableArray<PondUserModel *> *)ssArray{
        _ssArray = [NSMutableArray array];
    for (PondUserModel * model in self.Seledarray) {
        if(self.sstextfield.text.length > 0){
            if([model.GWName containsString:self.ssStr] || [model.realName containsString:self.ssStr] || [model.phone containsString:self.ssStr]){
                [_ssArray addObject:model];
            }
        } else {
            //            [_ssArray addObject:model];
        }
    }
    if(_ssArray.count < 1){
    
    } else {
        self.qdBtn.hidden = self.qxBtn.hidden = NO;
    }
    return _ssArray;
}
+ (instancetype)showviewWithTitle:(NSString *)title andArray:(NSArray <PositionUserModel *>*)array andBlock:(void (^)(NSString * ID))block{
    SearchPersonInCchargeShowView * view = [SearchPersonInCchargeShowView new];
    array.firstObject.isSeled = YES;
    view.array = array;
    view.title = title;
    for (PositionUserModel * model in array) {
        for (PondUserModel * usermodel in model.userModelList) {
            if(usermodel.isSeled){
                view.seledID = usermodel.userId;
            }
            BOOL isHave = NO;
            for(PondUserModel * ssmodel in view.Seledarray){
                if([ssmodel.userId isEqualToString:usermodel.userId]){
                    isHave = YES;
                    ssmodel.GWName = [NSString stringWithFormat:@"%@/%@",ssmodel.GWName,model.name];
                }
            }
            if(isHave == NO){
                usermodel.GWName = model.name;
                [view.Seledarray addObject:usermodel];
            }
        }
    }
    
    
    view.block = block;
    [view setupview];
    return view;
}
- (void)setupview{
    [[DTKTool getCurrentVC].view addSubview:self];
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.viewH = kwt(500) + KSafeAreaBottomHeight;
    UIView * makeView = [UIView new];
    self.makeView = makeView;
    makeView.backgroundColor = KcolorHex(@"#EDF2F8");
    makeView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, kwt(170) + KSafeAreaBottomHeight);
    [self addSubview:makeView];
    [DTKTool BFYJ:kwt(15) andView:makeView andCorner:UIRectCornerTopLeft|UIRectCornerTopRight andSize: CGRectMake(0, 0 ,SCREEN_WIDTH, self.viewH + KSafeAreaBottomHeight)];
    
//    view.userInteractionEnabled = YES;
//    [view addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:view action:@selector(dismiss)]];
//    makeView.userInteractionEnabled = YES;
//    [makeView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(noclick)]];
//    self.userInteractionEnabled = YES;
//    [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)]];
//    makeView.userInteractionEnabled = YES;
//    [makeView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(noclick)]];
    
    UILabel * titlab = [DTKTool labelWithText:self.title andFont:kCFont(18) andTextColor:KcolorHex(@"#242729")];
    [makeView addSubview:titlab];
    [titlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.top.offset(kwt(15));
    }];
 
    UIButton * btn = [UIButton buttonWithType:0];
    [btn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    btn.addTarget(self,@selector(dismiss));
    [makeView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-15));
        make.width.height.offset(kwt(20));
        make.centerY.equalTo(titlab);
    }];
    MJWeakSelf
    
    UIView * topview = [UIView new];
    topview.backgroundColor = UIColor.whiteColor;
    [makeView addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(kwt(50));
        make.height.offset(kwt(65));
    }];
    
    
    UIView * btnview = [UIView new];
    btnview.addClickBlock(^{
        [weakSelf openSsbtnview];
    });
    [DTKTool YJBK:kwt(5) andView:btnview andWidth:1 andColor:KcolorHex(@"#D4D5DA")];
    [topview addSubview:btnview];
    [btnview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.top.offset(kwt(11));
        make.height.offset(kwt(40));
    }];
    
    UIImageView * btnviewimg = kimageView(@"program_search");
    [btnview addSubview:btnviewimg];
    [btnviewimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.centerY.offset(0);
        make.height.width.offset(kwt(16));
    }];
    
    UILabel * btnviewlab = [DTKTool labelWithText:@"请搜索" andFont:kFont(16) andTextColor:KcolorHex(@"#89908D")];
    [btnview addSubview:btnviewlab];
    [btnviewlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(40));
        make.centerY.offset(0);
    }];
    
    UIView * btnline = [UIView new];
    btnline.backgroundColor = KcolorHex(@"#EAEBED");
    [btnview addSubview:btnline];
    [btnline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(280));
        make.centerY.offset(0);
        make.width.offset(0.5);
        make.height.offset(kwt(28));
    }];
    
    UILabel * btnviewsslab = [DTKTool labelWithText:@"搜索" andFont:kCFont(16) andTextColor:KcolorHex(@"#0063FC")];
    [btnview addSubview:btnviewsslab];
    [btnviewsslab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-16));
        make.centerY.offset(0);
    }];
    

    UIView * topline = [UIView new];
    topline.backgroundColor = KcolorHex(@"#E7E7E7");
    [topview addSubview:topline];
    [topline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(2));
        make.bottom.left.right.offset(0);

    }];
    

   
  
    UIView * buttonview = [UIView new];
    [makeView addSubview:buttonview];
    buttonview.backgroundColor = UIColor.whiteColor;
    [buttonview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(68) + KSafeAreaBottomHeight);
    }];
   
    UIButton * qxbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qxbtn.backgroundColor = KcolorHex(@"#F0F2F4");
    self.qxBtn = qxbtn;
    [qxbtn setTitle:@"取消" forState:UIControlStateNormal];
    [qxbtn setTitleColor:KcolorHex(@"#5A6066") forState:UIControlStateNormal];
    qxbtn.titleLabel.font = kFont(17);
    [DTKTool YJ:kwt(5) andView:qxbtn];
    [qxbtn addTarget:self action:@selector(leftBtnclick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:qxbtn];
    [qxbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(44));
        make.width.offset(kwt(161));
        make.left.offset(kwt(20));
        make.top.offset(kwt(12));
    }];
    
    UIButton * qrbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qrbtn.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(160), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#2892FE") endColor:KcolorHex(@"#0366FE")];
    self.qdBtn = qrbtn;

    [qrbtn setTitle:@"确定" forState:UIControlStateNormal];
    [qrbtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    qrbtn.titleLabel.font = kCFont(17);
    [DTKTool YJ:kwt(5) andView:qrbtn];
    [qrbtn addTarget:self action:@selector(rightbtnclick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:qrbtn];
    [qrbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(44));
        make.width.offset(kwt(161));
        make.right.offset(kwt(-20));
        make.top.offset(kwt(12));
    }];
  
    
    
    UITableView * lefttableview = [UITableView new];
    lefttableview.delegate = self;
    lefttableview.dataSource = self;
    lefttableview.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    lefttableview.sectionHeaderHeight = 0.01;
    lefttableview.backgroundColor = KcolorHex(@"#F0F3F9");
    [makeView addSubview:lefttableview];
    self.lefttableview = lefttableview;
    [lefttableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.width.offset(kwt(115));
        make.top.equalTo(topline.mas_bottom);
        make.bottom.equalTo(buttonview.mas_top);
    }];
    
    
    UITableView * righttableview = [UITableView new];
    righttableview.delegate = self;
    righttableview.dataSource = self;
    righttableview.backgroundColor = UIColor.whiteColor;
    righttableview.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    righttableview.sectionHeaderHeight = 0.01;
    [makeView addSubview:righttableview];
    self.righttableview = righttableview;
    [righttableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(0);
        make.left.equalTo(lefttableview.mas_right);
        make.top.equalTo(topline.mas_bottom);
        make.bottom.equalTo(buttonview.mas_top);
    }];
//   
    UIView * ssview = [UIView new];
    ssview.hidden = YES;
    self.ssview = ssview;
    ssview.backgroundColor = UIColor.whiteColor;
    [makeView addSubview:ssview];
    [ssview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(kwt(65));
        make.top.offset(0);
    }];
    
    
    UIButton * exitBtn = [UIButton buttonWithType:0];
    exitBtn.addTarget(self,@selector(openexitbtnview));
    exitBtn.addimg([UIImage imageNamed:@"left_black_path"]);
    [ssview addSubview:exitBtn];
    [exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.centerY.offset(0);
        make.width.height.offset(kwt(20));
    }];
    
    UIView * ssbjview = [UIView new];
    [DTKTool YJBK:kwt(5) andView:ssbjview andWidth:1 andColor:KcolorHex(@"#1971CF")];
    [ssview addSubview:ssbjview];
    [ssbjview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(50));
        make.right.offset(kwt(-15));
        make.top.offset(kwt(11));
        make.height.offset(kwt(40));
    }];
    
    UIImageView * ssimg = kimageView(@"program_search");
    [ssbjview addSubview:ssimg];
    [ssimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.centerY.offset(0);
        make.height.width.offset(kwt(16));
    }];
    
    UITextField * sstextfield = [UITextField new];
    [sstextfield addTarget:self action:@selector(textFieldDidChangeValue:)  forControlEvents:UIControlEventEditingChanged];
    sstextfield.placeholder = @"请搜索";
    sstextfield.font = kFont(16);
    sstextfield.returnKeyType = UIReturnKeySearch;

    self.sstextfield = sstextfield;
    [ssbjview addSubview:sstextfield];
    [sstextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(35));
        make.right.offset(kwt(-110));
        make.centerY.offset(0);
    }];
    
    UIButton * textRemovebtn = [UIButton buttonWithType:0];
    self.textRemovebtn = textRemovebtn;
    textRemovebtn.hidden = YES;
    textRemovebtn.addTarget(self,@selector(textRemovebtnClick));
    [textRemovebtn setImage:[UIImage imageNamed:@"search_delete"] forState:UIControlStateNormal];
    [ssbjview addSubview:textRemovebtn];
    [textRemovebtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(23));
        make.right.offset(kwt(-80));
        make.centerY.offset(0);
    }];
    
    
    UIView * ssline = [UIView new];
    ssline.backgroundColor = KcolorHex(@"#EAEBED");
    [ssbjview addSubview:ssline];
    [ssline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-65));
        make.centerY.offset(0);
        make.width.offset(0.5);
        make.height.offset(kwt(28));
    }];
    UIButton * ssbtn = [UIButton buttonWithType:0];
    ssbtn.addTarget(self,@selector(ssbtnClick));
    [ssbtn setTitle:@"搜索" forState:UIControlStateNormal];
    [ssbtn setTitleColor:KcolorHex(@"#0063FC") forState:UIControlStateNormal];
    ssbtn.titleLabel.font = kCFont(16);
    [ssbjview addSubview:ssbtn];
    [ssbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(0);
        make.left.equalTo(ssline);
        make.top.bottom.offset(0);
    }];
    
    UIView * ssbuttomline = [UIView new];
    ssbuttomline.backgroundColor = KcolorHex(@"#E7E7E7");
    [ssview addSubview:ssbuttomline];
    [ssbuttomline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(0.5);
        make.bottom.left.right.offset(0);

    }];
    
    UITableView * sstableview = [UITableView new];
    if (@available(iOS 11.0, *)) {
        sstableview.estimatedRowHeight = 0;
        sstableview.estimatedSectionHeaderHeight = 0;
        sstableview.estimatedSectionFooterHeight = 0;
        sstableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    if (@available(iOS 15.0, *)) {
        sstableview.sectionHeaderTopPadding = 0;
        sstableview.fillerRowHeight = 0;
    }
    sstableview.hidden = YES;
    sstableview.delegate = self;
    sstableview.dataSource = self;
    sstableview.emptyDataSetSource = self;
    sstableview.emptyDataSetDelegate = self;
    sstableview.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    sstableview.sectionHeaderHeight = 0.01;
    [makeView addSubview:sstableview];
    self.sstableview = sstableview;
    [sstableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(ssview.mas_bottom);
        make.bottom.equalTo(buttonview.mas_top);
    }];
//    
    [self ReloadUI];
    [self show];
    
}
- (void)textRemovebtnClick{
    self.sstextfield.text = @"";
    self.textRemovebtn.hidden = YES;
    self.qdBtn.hidden = self.qxBtn.hidden = NO;
    [self ReloadUI];
}
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{ return YES; }
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    //加入你自定义的view
    UIView * noView = [UIView new];
    if(self.sstextfield.text.length > 0){
        UIView * NewnoView = [UIView new];
        //    NewnoView.backgroundColor = UIColor.whiteColor;
        [noView addSubview:NewnoView];
        [NewnoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(KSCREEN_WIDTH);
            make.center.offset(0);
            make.height.mas_offset(KSCREEN_HEIGHT);
        }];
        UIImageView * noImage = [UIImageView new];
        [noView addSubview:noImage];
        noImage.image = [UIImage imageNamed:@"no_search"];
        [noImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_offset(0);
            make.centerY.mas_offset(-100);
        }];
        
        UILabel * noTitleLab = [UILabel new];
        [noView addSubview:noTitleLab];
        noTitleLab.text = @"暂无搜索记录\n换个关键词试试~";
        noTitleLab.numberOfLines = 2;
        noTitleLab.font = kFont(16);
        noTitleLab.textColor = [UIColor colorWithHexString:@"#44454C"];
        [noTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(noImage.mas_bottom).offset(30);
            make.centerX.mas_offset(0);
        }];
        self.qdBtn.hidden = self.qxBtn.hidden = YES;
    }
    return noView;
}
- (void)openSsbtnview{
    self.sstextfield.text = @"";
//    self.seledID = @"";
    [self.sstextfield becomeFirstResponder];
    self.sstableview.hidden = NO;
    self.ssview.hidden = NO;
    self.qdBtn.hidden = self.qxBtn.hidden = NO;
    [self.sstextfield becomeFirstResponder];
    [self ReloadUI];
    
}
- (void)openexitbtnview{
    self.qdBtn.hidden = self.qxBtn.hidden = NO;
    self.sstextfield.text = @"";
    self.textRemovebtn.hidden = YES;
//    self.seledID = @"";
    self.sstableview.hidden = YES;
    self.ssview.hidden = YES;
    [self.sstextfield resignFirstResponder];
    [self ReloadUI];
}
- (PositionUserModel *)seledModel{
    for (PositionUserModel * model in self.array) {
        if(model.isSeled){
            return model;
        }
    }
    return self.array.firstObject;
}
- (void)ReloadUI{
    [self.lefttableview reloadData];
    [self.righttableview reloadData];
    [self.sstableview reloadData];
}

- (void)leftBtnclick{
    [self dismiss];
    
}
- (void)ssbtnClick{
    [self.sstextfield resignFirstResponder];
    [self.sstableview reloadData];

}
- (void)rightbtnclick{
    if(self.seledID){
        if(self.block){
            self.block(self.seledID);
        }
    } else {
        [self showWarning:@"至少选择一人"];
        return;
    }
    
    [self dismiss];
}
- (void)textFieldDidChangeValue:(UITextField *)sender{
    if(sender.markedTextRange == nil){
//        NSLog(@"%@",sender.text);
        self.ssStr = sender.text;
        [self.sstableview reloadData];
    }
    self.textRemovebtn.hidden = (sender.text.length == 0);

}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [UIView new];
    view.backgroundColor = UIColor.whiteColor;
    view.clipsToBounds = YES;
    UILabel * lab = [DTKTool labelWithText:@"搜索结果" andFont:kFont(18) andTextColor:KcolorHex(@"#8D97A1")];
    [view addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.centerY.offset(0);
    }];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(tableView == self.sstableview && self.ssStr.length > 0 && self.ssArray.count > 0){
        return kwt(50);
    }
    return 0;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableView == self.lefttableview){
        return self.array.count;
    } else if(tableView == self.righttableview){
        return [self seledModel].userModelList.count;
    } else {
        return self.ssArray.count;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(tableView == self.sstableview){
        return kwt(77);
    }
    return kwt(53);
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MJWeakSelf
    if(tableView == self.lefttableview){
        
        PersonInCchargeLeftTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PersonInCchargeLeftTableViewCell"];
        if (cell == nil) {
            cell = [[PersonInCchargeLeftTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PersonInCchargeLeftTableViewCell"];
        }
        if(self.array.count > indexPath.row){
            cell.model = self.array[indexPath.row];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell ;
    } else if(tableView == self.righttableview){
        PersonInCchargeRightTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PersonInCchargeRightTableViewCell"];
        if (cell == nil) {
            cell = [[PersonInCchargeRightTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PersonInCchargeRightTableViewCell"];
        }
        cell.isDX = YES;
        if([self seledModel].userModelList.count > indexPath.row){
            cell.model = [self seledModel].userModelList[indexPath.row];
        }
        cell.isSeled = [[self seledModel].userModelList[indexPath.row].userId isEqualToString:self.seledID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell ;
    } else {
        SearchPersonInCchargeRightTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SearchPersonInCchargeRightTableViewCell"];
        if (cell == nil) {
            cell = [[SearchPersonInCchargeRightTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SearchPersonInCchargeRightTableViewCell"];
        }
        if(self.ssArray.count > indexPath.row){
            cell.model = self.ssArray[indexPath.row];

        }
        cell.isSeled = [self.ssArray[indexPath.row].userId isEqualToString:self.seledID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell ;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView == self.lefttableview){
        for (PositionUserModel * model in self.array) {
            model.isSeled = NO;
        }
        self.array[indexPath.row].isSeled = YES;
    } else  if(tableView == self.righttableview){
        PondUserModel * model = [self seledModel].userModelList[indexPath.row];
        self.seledID = model.userId;
    } else {
        PondUserModel * model = self.ssArray[indexPath.row];
        self.seledID = model.userId;
    }
    [self ReloadUI];
    
}
- (void)show{
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT - self.viewH , SCREEN_WIDTH, self.viewH);
    }];
}
- (void)noclick{}
- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT , SCREEN_WIDTH, self.viewH);
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}
@end


@interface SearchPersonInCchargeRightTableViewCell ()
@property (nonatomic ,strong) UIImageView * userimg;
@property (nonatomic ,strong) UILabel * namelab;
@property (nonatomic ,strong) UILabel * gwLab;

@end

@implementation SearchPersonInCchargeRightTableViewCell



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.whiteColor;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    UIImageView * userimg = kimageView(@"unslete_people");
    self.userimg= userimg;
    [self.contentView addSubview:userimg];
    [userimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(22));     
        make.top.left.offset(kwt(15));

    }];
    
    UILabel * namelab = [DTKTool labelWithText:@"-" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
    self.namelab = namelab;
    [self.contentView addSubview:namelab];
    [namelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(userimg);
        make.left.offset(kwt(42));
        make.right.offset(kwt(-42));
    }];
    
    UILabel * gwLab = [DTKTool labelWithText:@"-" andFont:kFont(15) andTextColor:KcolorHex(@"#8D97A1")];
    self.gwLab = gwLab;
    [self.contentView addSubview:gwLab];
    [gwLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(45));
        make.left.offset(kwt(42));
        make.right.offset(kwt(-42));
    }];
 
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#DEE0E2");
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.offset(0);
        make.height.offset(0.5);
    }];
}

- (void)setModel:(PondUserModel *)model{
    self.namelab.text = [NSString stringWithFormat:@"%@ %@",model.realName,model.phone];
    self.gwLab.text = model.GWName;
}
- (void)setIsSeled:(BOOL)isSeled{
    _isSeled = isSeled;
    if(isSeled){
        self.userimg.image = [UIImage imageNamed:@"slete_people"];
        self.namelab.textColor = KcolorHex(@"#0967FF");
        self.gwLab.textColor = KcolorHex(@"#0967FF");
    } else {
        self.namelab.textColor = KcolorHex(@"#222222");
        self.userimg.image = [UIImage imageNamed:@"unslete_people"];
        self.gwLab.textColor = KcolorHex(@"#8D97A1");
    }
}
@end
